#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void cmp_int(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;
}

void print_arr(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
void test1()
{
	int arr[10] = { 2,3,5,1,4,6,8,7,9,10 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_int);
	print_arr(arr, sz);
}

struct Stu
{
	char name[20];
	int age;
};

void get_name(const void* p1, const void* p2)
{
	return strcmp(((struct Stu*)p1)->name, ((struct Stu*)p2)->name);
}
void test2()
{
	struct Stu arr[3] = { {"zhangsan",11},{"lisi",39},{"wangwu",20} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), get_name);
}

void Swp(char* buf1, char* buf2)
{
	char temp = *buf1;
	*buf1 = *buf2;
	*buf2 = temp;
	buf1++;
	buf2++;
}
void Bubble_sort(void* base, size_t sz, size_t width, int(*cmp)(const void* p1, const void* p2))
{
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		int j = 0;
		for (j = 0; j < sz - i - 1; j++)
		{
			if (cmp((char*)base+j * width, (char*)base+(j + 1) * width) > 0)
			{
				Swp((char*)base + j * width, (char*)base + (j + 1) * width);
			}
		}
	}
}
void test3()
{
	int arr[10] = { 2,3,5,1,4,6,8,7,9,10 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	Bubble_sort(arr, sz, sizeof(arr[0]), cmp_int);
	print_arr(arr, sz);
}
int main()
{
	//test1();
	//test2();

	test3();
	return 0;
}